﻿using System.Data;
using FluentData;
namespace AdverseEventReportSystem.Core
{
    public class Db
    {
        #region 构造函数
        private static Db _instance;
        public static Db GetInstance()
        {
            if (_instance == null)
            {
                _instance = new Db();
            }
            return _instance;
        }
        #endregion
        #region Context

        public IDbContext Context(string connstr = "conn")
        {
            return new DbContext().ConnectionStringName(connstr,
                    new SqlServerProvider());
        }

        #endregion
        #region 分页语句拼接
        public string GetSqlForSelectBuilder(SelectBuilder data)
        {
            var sql = "";
            if (data.PagingCurrentPage <= 0) data.PagingCurrentPage = 1;
            if (data.PagingItemsPerPage <= 0) data.PagingItemsPerPage = 30;
            int startIndex = (data.PagingCurrentPage - 1) * data.PagingItemsPerPage + 1;
            int endIndex = startIndex + (data.PagingItemsPerPage - 1);
            sql = string.Format("select * from (select ROW_NUMBER() Over( " + data.OrderBy + @") as rowId,* from ({0}) as t ) as t1 
            	                           Where rowId between " + startIndex + " and " + endIndex, data.From);

            return sql;
        }
        public string GetSqlForTotalBuilder(SelectBuilder data)
        {
            string select = "select " + data.Select + " from " + data.From;
            if (data.WhereSql.Length > 0)
                select += " where " + data.WhereSql;
            if (data.GroupBy.Length > 0)
                select += " group by " + data.GroupBy;
            if (data.Having.Length > 0)
                select += " having " + data.Having;

            var sql = "select count(*) from (" + select + ") as t";
            return sql;
        }
        #endregion
        #region 查询语句拼接
        public string GetSelectSql(SelectBuilder data)
        {
            string select = "select " + data.Select + " from " + data.From;
            if (data.WhereSql != null && data.WhereSql.Length > 0)
                select += " where " + data.WhereSql;
            if (data.GroupBy != null && data.GroupBy.Length > 0)
                select += " group by " + data.GroupBy;
            if (data.Having != null && data.Having.Length > 0)
                select += " having " + data.Having;
            if (data.OrderBy != null && data.OrderBy.Length > 0)
                select += " order by " + data.OrderBy;

            return select;
        }
        #endregion
    }
}
#region SelectBuilder
public class SelectBuilder
{
    public int PagingCurrentPage { get; set; }
    public int PagingItemsPerPage { get; set; }
    public string Having { get; set; }
    public string GroupBy { get; set; }
    public string OrderBy { get; set; }
    public string From { get; set; }
    public string Select { get; set; }
    public string WhereSql { get; set; }
}
#endregion